package com.vova.rec.grpc.client.config;

import com.vova.rec.grpc.common.DeviceFixServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class ClientConfig {

    @Bean
    public ManagedChannel managedChannel() {
        Map<String, Object> retryPolicy = new HashMap<>();
        retryPolicy.put("maxAttempts", 2D);
        retryPolicy.put("initialBackoff", "0.1s");
        retryPolicy.put("maxBackoff", "1s");
        retryPolicy.put("backoffMultiplier", 2D);
        retryPolicy.put("retryableStatusCodes", Arrays.asList("UNAVAILABLE", "UNAUTHENTICATED", "DEADLINE_EXCEEDED",
                "UNKNOWN", "UNIMPLEMENTED"));
        Map<String, Object> methodConfig = new HashMap<>();
        Map<String, Object> name = new HashMap<>();
        name.put("service", "com.vova.rec.grpc.common.DeviceFixService");
        methodConfig.put("name", Collections.singletonList(name));
        methodConfig.put("retryPolicy", retryPolicy);
        Map<String, Object> serviceConfig = new HashMap<>();
        serviceConfig.put("methodConfig", Collections.singletonList(methodConfig));

        return ManagedChannelBuilder.forAddress("localhost", 9090)
                // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid needing certificates.
                .usePlaintext()
                .defaultServiceConfig(serviceConfig)
                .enableRetry()
                .build();
    }

//    @PostConstruct
//    public void init() {
//        Map<String, Object> retryPolicy = new HashMap<>();
//        retryPolicy.put("maxAttempts", 2D);
//        retryPolicy.put("initialBackoff", "0.1s");
//        retryPolicy.put("maxBackoff", "1s");
//        retryPolicy.put("backoffMultiplier", 2D);
//        retryPolicy.put("retryableStatusCodes", Arrays.asList("UNAVAILABLE", "UNAUTHENTICATED", "DEADLINE_EXCEEDED", "UNKNOWN"));
//        Map<String, Object> methodConfig = new HashMap<>();
//        Map<String, Object> name = new HashMap<>();
//        name.put("service", "com.vova.rec.grpc.common.DeviceFixService");
//        methodConfig.put("name", Collections.singletonList(name));
//        methodConfig.put("retryPolicy", retryPolicy);
//        Map<String, Object> serviceConfig = new HashMap<>();
//        serviceConfig.put("methodConfig", Collections.singletonList(methodConfig));
//        this.channel = ManagedChannelBuilder.forAddress("localhost", 9090)
//                // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid needing certificates.
//                .usePlaintext()
//                .defaultServiceConfig(serviceConfig)
//                .enableRetry()
//                .build();
//    }

    @Bean
    public DeviceFixServiceGrpc.DeviceFixServiceBlockingStub deviceFixServiceBlockingStub(ManagedChannel managedChannel) {
        return DeviceFixServiceGrpc.newBlockingStub(managedChannel);
    }

}
